package cn.mw.cmdb.controller;

import cn.mw.cmdb.constants.FinanceReportConstants;
import cn.mw.cmdb.entity.FinanceReport.*;
import cn.mw.cmdb.service.FinanceReportDictionaryService;
import cn.mw.cmdb.service.FinanceReportMappingRuleService;
import cn.mw.cmdb.util.GZIPUtils;
import cn.mw.components.mongodb.entity.SearchParam;
import cn.mw.microMonitorCommon.api.common.BaseApiService;
import cn.mw.microMonitorCommon.api.common.ResponseBase;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RequestMapping("/financeReport")
@RestController
@Tag(name = "金融元上报")
@Slf4j
public class FinanceReportController extends BaseApiService {

    @Autowired
    private FinanceReportDictionaryService financeReportDictionaryService;

    @Autowired
    private FinanceReportMappingRuleService financeReportMappingRuleService;

    @PostMapping("/addOrUpdateFinanceReportGroupInfo")
    @ResponseBody
    @Operation(summary = "新增或编辑分组")
    public ResponseBase addOrUpdateFinanceReportGroupInfo(@RequestBody FinanceReportGroupInfo financeReportGroupInfo) throws Exception {
        return setResultSuccess(financeReportDictionaryService.addOrUpdateFinanceReportGroupInfo(financeReportGroupInfo));
    }

    @GetMapping("/deleteFinanceReportGroupInfo")
    @ResponseBody
    @Operation(summary = "删除分组")
    public ResponseBase deleteFinanceReportGroupInfo(String id) throws Exception {
        return setResultSuccess(financeReportDictionaryService.deleteFinanceReportGroupInfo(id));
    }

    @PostMapping("/getFinanceGroupTree")
    @ResponseBody
    @Operation(summary = "分组树形结构")
    public ResponseBase getFinanceGroupTree(Integer channelId) throws Exception {
        return setResultSuccess(financeReportDictionaryService.getGroupTree(channelId));
    }

    @GetMapping("/getFinanceGroupInfo")
    @ResponseBody
    @Operation(summary = "查询分组信息")
    public ResponseBase getFinanceGroupInfo(String id) throws Exception {
        return setResultSuccess(financeReportDictionaryService.getGroupInfo(id));
    }

    @PostMapping("addOrUpdateFinanceReportDictionary")
    @ApiOperation(value = "新增或修改上报通道字典")
    public ResponseBase<Boolean> addOrUpdateFinanceReportDictionary(@RequestBody FinanceReportDictionaryInfo financeReportDictionaryInfo) throws Exception {
        return setResultSuccess(financeReportDictionaryService.addOrUpdateFinanceReportDictionary(financeReportDictionaryInfo));
    }

    @PostMapping("batchDeleteFinanceReportDictionaryInfo")
    @ApiOperation(value = "批量删除上报通道字典")
    public ResponseBase<Boolean> batchDeleteFinanceReportDictionaryInfo(@RequestBody List<String> ids) throws Exception {
        return setResultSuccess(financeReportDictionaryService.batchDeleteFinanceReportDictionaryInfo(ids));
    }

    @PostMapping("getFinanceReportDictionaryInfoByPage")
    @ApiOperation(value = "分页查询上报通道字典")
    public ResponseBase<IPage<FinanceReportDictionaryInfo>> getFinanceReportDictionaryInfoByPage(@RequestBody SearchParam param) throws Exception {
        return setResultSuccess(financeReportDictionaryService.getFinanceReportDictionaryInfoByPage(param));
    }

    @PostMapping("addOrUpdateFinanceReportMappingRule")
    @ApiOperation(value = "新增或修改上报通道映射规则")
    public ResponseBase<Boolean> addOrUpdateFinanceReportMappingRule(@RequestBody FinanceReportMappingRuleInfo financeReportMappingRuleInfo) throws Exception {
        return setResultSuccess(financeReportMappingRuleService.addOrUpdateFinanceReportMappingRule(financeReportMappingRuleInfo));
    }

    @PostMapping("batchDeleteFinanceReportMappingRule")
    @ApiOperation(value = "批量删除上报通道映射规则")
    public ResponseBase<Boolean> batchDeleteFinanceReportMappingRule(@RequestBody List<String> ids) throws Exception {
        return setResultSuccess(financeReportMappingRuleService.batchDeleteFinanceReportMappingRule(ids));
    }

    @PostMapping("getFinanceReportMappingRuleByPage")
    @ApiOperation(value = "分页查询上报通道映射规则")
    public ResponseBase<IPage<FinanceReportMappingRuleInfo>> getFinanceReportMappingRuleByPage(@RequestBody SearchParam param) throws Exception {
        return setResultSuccess(financeReportMappingRuleService.getFinanceReportMappingRuleByPage(param));
    }

    @GetMapping("getReportTree")
    @ApiOperation(value = "获取金融元上报目录树")
    public ResponseBase<Object> getReportTree(Integer channelId) throws Exception {
        return setResultSuccess(financeReportMappingRuleService.getReportTree(channelId));
    }

    @PostMapping("getApplicationScopeTree")
    @ApiOperation(value = "获取金融元应用范围选择树")
    public ResponseBase<Object> getApplicationScopeTree(@RequestBody List<Integer> channelIdList) throws Exception {
        return setResultSuccess(financeReportMappingRuleService.getApplicationScopeTree(channelIdList));
    }

    @PostMapping("reportCheck")
    @ApiOperation(value = "上报前校验")
    public ResponseBase<Object> reportCheck(Integer channelId, @RequestBody List<String> instanceIdList) throws Exception {
        return setResultSuccess(financeReportMappingRuleService.reportCheck(channelId, instanceIdList, null));
    }

    @PostMapping("doReport")
    @ApiOperation(value = "上报")
    public ResponseBase<Object> doReport(@RequestBody FinanceReportCheckResultVo financeReportCheckResultVo) throws Exception {
        return setResultSuccess(financeReportMappingRuleService.doReport(financeReportCheckResultVo));
    }

    @PostMapping("getFinanceReportHistoryInfoByPage")
    @ApiOperation(value = "分页查询上报历史记录")
    public ResponseBase<IPage<FinanceReportMappingRuleInfo>> getFinanceReportHistoryInfoByPage(@RequestBody SearchParam param) throws Exception {
        return setResultSuccess(financeReportMappingRuleService.getFinanceReportHistoryInfoByPage(param));
    }

    @PostMapping("getFinanceReportHistoryStatusCount")
    @ApiOperation(value = "上报历史按状态统计")
    public ResponseBase<Map<String, Integer>> getFinanceReportHistoryStatusCount(@RequestBody SearchParam param) throws Exception {
        return setResultSuccess(financeReportMappingRuleService.getFinanceReportHistoryStatusCount(param));
    }

    @PostMapping("batchDeleteFinanceReportHistory")
    @ApiOperation(value = "批量删除上报历史")
    public ResponseBase<Boolean> batchDeleteFinanceReportHistory(@RequestBody List<String> ids) throws Exception {
        return setResultSuccess(financeReportMappingRuleService.batchDeleteFinanceReportHistory(ids));
    }

    @GetMapping("queryReportResult")
    @ApiOperation(value = "查询上报结果")
    public ResponseBase<Boolean> queryReportResult(String historyId) throws Exception {
        return setResultSuccess(financeReportMappingRuleService.queryReportResult(historyId));
    }

    @GetMapping("reDoReport")
    @ApiOperation(value = "重新上报")
    public ResponseBase<Object> reDoReport(String historyId) throws Exception {
        return setResultSuccess(financeReportMappingRuleService.reDoReport(historyId));
    }

    // Todo 模拟接口 联调通过后需要删除
    @PostMapping("/webproxy/fig2fics/oauth2/v1/pshare/oauth/token")
    @ApiOperation(value = "模拟金融元上报Token获取")
    public Map<String, Object> getReportTokenMock(@RequestBody String requestBody) throws Exception {
        Map<String, Object> mockResult = new HashMap<>();
        mockResult.put("access_token", "token");
        return mockResult;
    }

    // Todo 模拟接口 联调通过后需要删除
    @PostMapping("/getRequestParamCompressMock")
    @ApiOperation(value = "模拟金融元上报压缩参数")
    public Map<String, Object> getRequestParamCompressMock(@RequestBody FinanceReportRequestParam financeReportRequestParam) throws Exception {
        Map<String, Object> mockResult = new HashMap<>();
        String compressedBody = GZIPUtils.compress(JSON.toJSONString(financeReportRequestParam));
        mockResult.put("compressedBody", compressedBody);
        mockResult.put("Content-Length", String.valueOf(compressedBody.length()));
        return mockResult;
    }

    // Todo 模拟接口 联调通过后需要删除
    @PostMapping("/webproxy/fig2fics/pshare/api/prod/FICS/api/fics/dataElementInstance/reportData")
    @ApiOperation(value = "模拟金融元上报")
    public FinanceReportResponseVo doReportMock(@RequestBody String paramsStr) throws Exception {

        FinanceReportRequestParam financeReportRequestParam = JSON.parseObject(GZIPUtils.uncompress(paramsStr), FinanceReportRequestParam.class);

        FinanceReportResponseVo financeReportResponseVo = new FinanceReportResponseVo();
        financeReportResponseVo.setCode("WL-10000");
        financeReportResponseVo.setMsg("接收成功");
        if (financeReportRequestParam.getBranchId() == null) {
            financeReportResponseVo.setBranchId(UUID.randomUUID().toString());
        } else {
            financeReportResponseVo.setBranchId(financeReportRequestParam.getBranchId());
        }
        return financeReportResponseVo;
    }

    // Todo 模拟接口 联调通过后需要删除
    @PostMapping("/webproxy/fig2fics/pshare/api/prod/FICS/api/fics/dataElementInstance/selectUploadData")
    @ApiOperation(value = "模拟金融元结果查询")
    public FinanceReportResponseVo doReportQueryMock(@RequestBody FinanceReportRequestParam financeReportRequestParam) throws Exception {
        FinanceReportResponseVo financeReportResponseVo = new FinanceReportResponseVo();
        financeReportResponseVo.setCode(FinanceReportConstants.QueryStatus.SAVE_SUCCESS);
        financeReportResponseVo.setMsg("已保存");
        return financeReportResponseVo;
    }


}
